package com.itheima.controller;

import com.itheima.Service.UserOrderService;
import com.itheima.pojo.Order;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@Slf4j
@RestController
@RequestMapping("/order/user")
public class UserOrderController {
    @Autowired
    private UserOrderService userOrderService;

    /**
     * 修改用户订单
     * @param userOrder
     * @return
     */
    @PutMapping
    public Result updateUserOrder(@RequestBody Order userOrder, HttpServletRequest request){
        String token = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(token);
        Integer userId = (Integer) claims.get("id");
        userOrder.setUpdateBy(Long.valueOf(userId));
        userOrderService.updateUserOrder(userOrder);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        Order order = userOrderService.getById(id);
        return Result.success(order);
    }

    @GetMapping("/page")
    public Result page(Order query){
        PageBean pageBean = userOrderService.page(query);
        return Result.success(pageBean);
    }
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除订单{}", id);
        userOrderService.delete(id);
        return Result.success();
    }
    @PostMapping
    public Result add(@RequestBody Order order) {

       userOrderService.add(order);
        return Result.success();
    }

}
